import { ref } from 'vue';
import { Input, message } from 'ant-design-vue';
import { useAuthStore } from '@/store/modules/auth';
import { isNonNegativeInteger } from '@/utils';
import { get } from '@/service/api/api';
let topFormModel;
let bottomFormModel;
const lotType = ref('input');
const lotids = ref([]);
const moduleNoSearchData = ref(null);

// // 在数据加载完成后赋值
// async function loadModuleData() {
//   const data = await fetchModuleData();
//   moduleNoSearchData.value = data;
// }

// // 在组件加载时调用
// onMounted(() => {
//   loadModuleData();
// });
const authStore = useAuthStore();
export default {
  url: '/qms/single/singlePunch/list',
  addUrl: '/qms/single/singlePunch/add',
  editUrl: '/qms/single/singlePunch/update',
  delUrl: '/qms/single/singlePunch/deleteBatch',
  modalGetTableDataUrl: '/qms/single/singlePunch/',
  searchFormList: [
    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'lotId',
      label: 'LOT号',
      placeholder: '请输入LOT号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },
    {
      type: 'dateRangePicker',
      key: 'time',
      label: '时间',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '生产型号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'lotId',
      dataIndex: 'lotId',
      title: 'LOT号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'time',
      dataIndex: 'time',
      title: '时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'machineNo',
      dataIndex: 'machineNo',
      title: '机台号',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'dieToolNum',
      dataIndex: 'dieToolNum',
      title: '冲模工具号',
      width: 190,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'appearTolerance',
      dataIndex: 'appearTolerance',
      title: '外形公差',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'boardLength',
      dataIndex: 'boardLength',
      title: '要求长',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'boardWidth',
      dataIndex: 'boardWidth',
      title: '要求宽',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'actuallyLong',
      dataIndex: 'actuallyLong',
      title: '实际长',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'actuallyWidth',
      dataIndex: 'actuallyWidth',
      title: '实际宽',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'result',
      dataIndex: 'result',
      title: '判定结果',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkType1',
      dataIndex: 'checkType1',
      title: '首件检查',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkType2',
      dataIndex: 'checkType2',
      title: '自主检查',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkType3',
      dataIndex: 'checkType3',
      title: '末件检查',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'operator',
      dataIndex: 'operator',
      title: '操作员',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'shiftType',
      dataIndex: 'shiftType',
      title: '班别',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['白班', '夜班'][record.shiftType];
      }
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],
  searchParam: ref({
    moduleNo: null,
    lotId: null,
    state: null,
    time: null
  }),
  scroll: { x: 2500, y: 'auto' },
  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },
    {
      type: 'searchInput',
      key: 'moduleNo',
      label: '生产型号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      disabled: false,
      rule: {
        required: true,
        message: '请输入生产型号',
        trigger: 'change'
      },
      transfromSearch(model, searchData) {
        topFormModel = model;
        lotids.value = searchData.lotNo.map(item => {
          return {
            // label: `${item.techName} - ${item.lotNo}`,
            label: `${item.lotNo}`,
            value: `${item.lotNo}`
          };
        });
        moduleNoSearchData.value = searchData;
        lotType.value = 'select';
        model.value.lotId = null;
        model.value.quantity = 2;
        model.value.speed = 550;
        model.value.hotTemperature = 85;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
      }
    },
    {
      type: lotType,
      key: 'lotId',
      label: 'Lot号',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      span: 8,
      selectOptions: lotids,
      rule: {
        required: true,
        message: '请输入Lot号',
        trigger: 'change'
      }
    },
    {
      type: 'datePicker',
      key: 'time',
      label: '日期',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true,
      rule: {
        required: true,
        message: '请选择日期',
        trigger: 'change'
      }
    },

    {
      type: 'input',
      key: 'machineNo',
      label: '机台号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入机台号',
        trigger: 'change'
      }
    },

    {
      type: 'input',
      key: 'dieToolNum',
      // label: '治具型号',
      label: '冲模工具号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入冲模工具号',
        trigger: 'change'
      }
      // change(e){
      //   model.value.hotTemperature =
      // }
    },
    {
      type: 'input',
      key: 'appearTolerance',
      label: '外形公差',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入外形公差',
        trigger: 'change'
      }
    },
    {
      type: 'input',
      key: 'boardLength',
      label: '要求首件外形长度(MM)',
      span: 12,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入要求首件外形长度(MM)',
        trigger: 'change'
      }
    },
    {
      type: 'input',
      key: 'boardWidth',
      label: '要求首件外形宽度(MM)',
      span: 12,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入要求首件外形宽度(MM)',
        trigger: 'change'
      }
    },
    {
      type: 'input',
      key: 'actuallyLong',
      label: '实际首件外形长度(MM)',
      span: 12,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入实际首件外形长度(MM)',
        trigger: 'change'
      },
      async blur(e, val) {
        console.log('e', e);
        console.log('val', val);
        try {
          // 可选链操作符
          const requireData = topFormModel.value?.boardLength;
          const appearTolerance = topFormModel.value?.appearTolerance;
          const response = await get('/qms/single/singlePunch/checkData', {
            actualData: val,
            requireData,
            appearTolerance
          });
          console.log('response', response.data);
          if (!response.data) {
            message.error('超出界限');
            e.target.style.color = 'red';
          } else {
            e.target.style.color = ''; // 恢复默认颜色
          }
        } catch (error) {
          message.error('请求失败，请检查网络或数据');
        }
      }
    },
    {
      type: 'input',
      key: 'actuallyWidth',
      label: '实际首件外形宽度(MM)',
      span: 12,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      rule: {
        required: true,
        message: '请输入实际首件外形宽度(MM)',
        trigger: 'change'
      },
      async blur(e, val) {
        console.log('e', e);
        console.log('val', val);
        try {
          // 可选链操作符
          const requireData = topFormModel.value?.boardWidth;
          const appearTolerance = topFormModel.value?.appearTolerance;
          const response = await get('/qms/single/singlePunch/checkData', {
            actualData: val,
            requireData,
            appearTolerance
          });
          console.log('response', response.data);
          if (!response.data) {
            message.error('超出界限');
            e.target.style.color = 'red';
          } else {
            e.target.style.color = ''; // 恢复默认颜色
          }
        } catch (error) {
          message.error('请求失败，请检查网络或数据');
        }
      }
    },
    {
      type: 'radio',
      key: 'checkType1',
      label: '首件检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showReset: true,
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },

    {
      type: 'radio',
      key: 'checkType2',
      label: '自主检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showReset: true,
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },

    {
      type: 'radio',
      key: 'checkType3',
      label: '末件检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showReset: true,
      showReset: true,
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },

    {
      type: 'input',
      key: 'checkNum1',
      label: '首件检查数量',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      span: 8
    },
    {
      type: 'input',
      key: 'checkNum2',
      label: '自主检查数量',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      span: 8
    },
    {
      type: 'input',
      key: 'checkNum3',
      label: '末件检查数量',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      span: 8
    },
    {
      type: 'radio',
      key: 'result',
      label: '判定结果',
      span: 8,
      rule: {
        required: true,
        message: '请选择判定结果',
        trigger: 'blur'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'ACC',
          value: 'ACC'
        },
        {
          label: 'UAI',
          value: 'UAI'
        },
        {
          label: 'REJ',
          value: 'REJ'
        }
      ]
    },
    {
      type: 'radio',
      key: 'shiftType',
      label: '班别',
      span: 12,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '白班',
          value: '0'
        },
        {
          label: '夜班',
          value: '1'
        }
      ],
      rule: {
        required: true,
        message: '请选择班别',
        trigger: 'blur'
      }
    },
    {
      type: 'textarea',
      key: 'equipmentFailureStatistics',
      label: '设备故障统计(故障时间一修复时间)',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 12
    },
    {
      type: 'textarea',
      key: 'qualityAnomalyStatistics',
      label: '品质异常统计(当班次品质异常)',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 12
    },
    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'operator',
      label: '操作员',
      defaultValue: authStore.userInfo.user.nickName || authStore.userInfo.user.userName,
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 8
    }
  ],
  modalHeight: '90vh',
  modalTableKey: 'badId',
  modalAddTableKey: 'badDoList',
  modalTableColumn: [
    {
      key: 'index',
      dataIndex: 'index',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'badName',
      dataIndex: 'badName',
      title: '检验项目',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'number',
      dataIndex: 'number',
      title: '缺陷数量',
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        if (record.disabled) {
          return <div style="min-width:150px">{record.number}</div>;
        }
        return (
          <Input
            style="height: 22px !important;"
            value={record.number}
            onBlur={e => {
              const value = e.target.value;
              if (!(isNonNegativeInteger(Number(value).toString()) && Number(value >= 0))) {
                message.error('请输入正整数');
                record.number = null;
                return;
              }
              record.number = Number(value);
            }}
          ></Input>
        );
      }
    }
  ],
  modalClose() {
    lotType.value = 'input';
    moduleNoSearchData.value = null;
    lotids.value = [];
  }
};
